package com.woniu.stafforder.controller;

import com.woniu.common.util.ResponseData;
import com.woniu.stafforder.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 支付宝支付
 */
@RestController
public class AlipayController {
    @Autowired
    private PayService payService;

    /**
     * 生成支付宝付款码页面
     * @param orderNum 订单编号
     * @return 支付宝页面
     */
    @RequestMapping("/alipay/getPayHtml")
    public String getPayHtml(String orderNum) {
        return payService.getPayHtml(orderNum);
    }

    /**
     * 退款
     * @param orderNum 订单编号
     * @return 结果
     */
    @RequestMapping("/alipay/refund")
    public ResponseData<?> refund(String orderNum) {
        return payService.refund(orderNum);
    }

    @RequestMapping("/alipay/notify")
    public void notify(@RequestParam("orderNum") String orderNum) {
        System.out.println("异步回调成功");
    }

    /**
     * 付款成功
     * @param request 请求
     * @return 响应成功结果
     */
    @RequestMapping("/alipay/paySuccess")
    public ResponseData<?> paySuccess(HttpServletRequest request) {
        // 从请求获取订单id
        String orderNum = request.getParameter("out_trade_no");
        payService.paySuccess(orderNum);
        return new ResponseData<>().ok();
    }
}

